package com.sdgakj.web.h5;

import com.google.common.collect.Maps;
import com.sdgakj.dto.FeedCommentDTO;
import com.sdgakj.dto.FeedDTO;
import com.sdgakj.entity.*;
import com.sdgakj.enums.EnumFeedType;
import com.sdgakj.rest.UserLoginRestController;
import com.sdgakj.service.account.ShiroDbRealm;
import com.sdgakj.service.feed.FeedService;
import com.sdgakj.service.feed.ShareService;
import com.sdgakj.service.history.HistoryService;
import com.sdgakj.service.notes.NotesService;
import com.sdgakj.service.person.PersonService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/h5/feed")
public class H5FeedController {

	private static final String PAGE_SIZE = "10";
	private static Map<String,String> sortTypes =Maps.newLinkedHashMap();
	private static Logger logger = LoggerFactory.getLogger(UserLoginRestController.class);

	@Autowired
	private NotesService notesService;
	@Autowired
	private HistoryService historyService;
	@Autowired
	private ShareService shareService;
	@Autowired
	private FeedService feedService;
	@Autowired
	private PersonService personService;

	static {
		sortTypes.put("auto", "自动");
	}

    /**
     * 更新管理员用户信息
     * @return
     */
    @RequestMapping(value="/list",method=RequestMethod.GET)
    public String userJsp(){
		return "h5/index/index";
    }

	/**
	 * 大事记列表页
	 * @return
	 */
	@RequestMapping(value="/dashiji",method=RequestMethod.GET)
	public String dashiji(@RequestParam(value = "familyId", required = false) Long familyId,
						  @RequestParam(value = "page", defaultValue = "1") int pageNumber,
						  @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
						  @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model){
			Map<String, Object> searchParams =new HashMap<>();
			searchParams.put("EQ_fId", "1");
			Page<Notes> notes = notesService.getNotesByPage(searchParams,pageNumber,pageSize,"",sortType);
			model.addAttribute("notes",notes);
			return "h5/feed/dashijiList";
	}

	/**
	 * 大事记详情页
	 * @return
	 */
	@RequestMapping(value="/dashijiDetail",method=RequestMethod.GET)
	public String dashiji(@RequestParam(value = "id", required = false) Long noteId,Model model){
		Map<String, Object> searchParams =new HashMap<>();
		searchParams.put("EQ_noteId", noteId.toString());
		Notes note = notesService.getNoteById(noteId);
		model.addAttribute("note",note);
		return "h5/feed/dashijiDetail";
	}
	/**
	 * 历史列表页面
	 * @param pageNumber 页号
	 * @param pageSize   每页大小
	 * @param sortType   排序字段
	 * @return
	 */
	@RequestMapping(value="/history",method=RequestMethod.GET)
	public String histories(@RequestParam(value = "familyId", required = false) Long familyId,
						  @RequestParam(value = "page", defaultValue = "1") int pageNumber,
						  @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
						  @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model){
		Map<String, Object> searchParams =new HashMap<>();
		searchParams.put("EQ_id", "1");
		Page<History> histories = historyService.getHistoriesByPage(searchParams,pageNumber,pageSize,"",sortType);
		model.addAttribute("histories",histories);
		return "h5/feed/historyList";
	}
	/**
	 * 历史列表详情页
	 * @return
	 */
	@RequestMapping(value="/historyDetail",method=RequestMethod.GET)
	public String history(@RequestParam(value = "id", required = false) Long id,Model model){
		Map<String, Object> searchParams =new HashMap<>();
		searchParams.put("EQ_id", 1);
		History history = historyService.getHistoryById(id);
		model.addAttribute("history",history);
		return "h5/feed/historyDetail";
	}
	/**
	 * 分享列表页
	 * @return
	 */
	@RequestMapping(value="/share",method=RequestMethod.GET)
	public String share(@RequestParam(value = "familyId", required = false) Long familyId,
						  @RequestParam(value = "page", defaultValue = "1") int pageNumber,
						  @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
						  @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model){
		Map<String, Object> searchParams =new HashMap<String,Object>();
/*
		searchParams.put("EQ_id", "1");
*/
		Page<Share> shares = shareService.getSharesByPage(searchParams,pageNumber,pageSize,"",sortType);
		model.addAttribute("shares",shares);
		return "h5/feed/shareList";
	}
	/**
	 * 分享列表详情页
	 * @return
	 */
	@RequestMapping(value="/shareDetail",method=RequestMethod.GET)
	public String share(@RequestParam(value = "id", required = false) Long id,Model model){
		Map<String, Object> searchParams =new HashMap<>();
		searchParams.put("EQ_id", id);

		Share share = shareService.getShareById(id);
		model.addAttribute("share",share);
		return "h5/feed/shareDetail";
	}

	/**
	 * 更新管理员用户信息
	 * @return
	 */
	@RequestMapping(value="/actives",method=RequestMethod.GET)
	public String actives(@RequestParam(value = "familyId", required = false) Long familyId){
		return "h5/feed/actives";
	}
	/**
	 * 更新管理员用户信息
	 * @return
	 */
/*	@RequestMapping(value="/share",method=RequestMethod.GET)
	public String share(@RequestParam(value = "familyId", required = false) Long familyId,
						  @RequestParam(value = "userId", required = false) Long userId){
		return "h5/feed/share";
	}*/
	/**
	 * 更新管理员用户信息
	 * @return
	 */
	/*@RequestMapping(value="/history",method=RequestMethod.GET)
	public String history(@RequestParam(value = "familyId", required = false) Long familyId,
						@RequestParam(value = "userId", required = false) Long userId){
		return "h5/feed/history";
	}*/

	/**
	 * feed消息
	 * 首页消息详情页
	 * @return
	 */
	@RequestMapping(value="/feedDetail",method=RequestMethod.GET)
	public String feedDetail(@RequestParam(value = "userId",required = false) Long userId,
							 @RequestParam(value = "id", required = false) Long id,
							 @RequestParam(value = "tagid", required = true ) Long tagId,
							 Model model) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		searchParams.put("EQ_id", id.toString());
		FeedDTO feed=this.feedService.getFeedOne(userId,id);
		Integer pageSize=feedService.getFeedCommentCount(id,userId);
		Person person=personService.getPersonByUserId(userId);
		String request="";
		if (tagId == 1) {
			request="h5/family/fcircleList";//家族圈
		} else if (tagId == 2) {
			request="h5/index/message_note";//大事记
		} else if (tagId == 3) {
			request= "h5/index/message_dynamic";//家族动态
		} else if (tagId == 4) {
			request= "h5/index/share";//分享
		}else if (tagId == 5){
			request="h5/user/process_detail";//历程
		}
		if(tagId!=5) {
			List<FeedCommentDTO> feedComments=feedService.getCommentListTimeline(id,null,null,pageSize);
			//System.out.println(feedComments.size());
			model.addAttribute("four", feedComments);
		}
		model.addAttribute("person",person);
		model.addAttribute("feed", feed);
		return request;
	}
}
